package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Brand;
import cn.wolfcode.domain.Product;
import cn.wolfcode.qo.ProductQueryObject;
import cn.wolfcode.qo.pageResult;
import cn.wolfcode.service.IBrandService;
import cn.wolfcode.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName productController
 * @Description TODO
 * @Author wdx
 * @Date 2023/3/31 0:19
 */
@Controller
@RequestMapping("/product")
public class productController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IBrandService brandService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")ProductQueryObject qo){
        pageResult<Product> pageResult = productService.Query(qo);
        model.addAttribute("pageResult",pageResult);
        List<Brand> brands = brandService.selectAll();
        model.addAttribute("brands",brands);
        return "product/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id != null){
            productService.delete(id);
        }
        return "redirect:/product/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Brand> brands = brandService.selectAll();
        model.addAttribute("brands",brands);
        if(id != null){
            Product product = productService.selectById(id);
            model.addAttribute("product",product);
        }
        return "product/input";
    }
    @RequestMapping("/saveOrupdate")
    public String saveOrupdate(Product product){
    if(product.getId() != null){
        productService.update(product);
    }
    else{
        productService.insert(product);
    }
    return "redirect:/product/list";
    }
}
